package com.zwy.core.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zwy.core.domain.entity.TbUser;
import com.zwy.core.mapper.TbUserMapper;
import com.zwy.core.mapper.TbUserRoleMapper;
import com.zwy.core.service.ITbRoleService;
import com.zwy.core.service.ITbUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周哥
 * @since 2025-06-08
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements ITbUserService {


//    private final TbUserMapper userMapper;
//    private final PasswordEncoder passwordEncoder;
//    private final ITbRoleService roleService;
//
//    public TbUserServiceImpl(TbUserMapper userMapper, PasswordEncoder passwordEncoder, ITbRoleService roleService) {
//        this.userMapper = userMapper;
//        this.passwordEncoder = passwordEncoder;
//        this.roleService = roleService;
//    }

    @Autowired
    private TbUserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private ITbRoleService roleService;

    @Override
    public TbUser findByUsername(String username) {
        return userMapper.selectOne(new LambdaQueryWrapper<TbUser>()
                .eq(TbUser::getUsername,username));
    }
    @Override
    public TbUser saveUser(TbUser user){
        //密码加密
        if(ObjectUtil.isEmpty(user.getPassword())){
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }
        if(ObjectUtil.isEmpty(user.getId())){
            userMapper.insert(user);
        }else {
            userMapper.updateById(user);
        }
        return user;
    }
}
